package io.gitee.chearnee.fw.dingtalk.msg;

import cn.hutool.core.date.LocalDateTimeUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.skywalking.apm.toolkit.trace.TraceContext;
import org.springframework.util.StringUtils;

import java.text.MessageFormat;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 2022/8/31 15:31
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class DingTalkMessage extends DingTalkBaseMessage {

  @Override
  public String toMessage() {
    String traceId = TraceContext.traceId();
    String errorStackString = "无";
    if (StringUtils.hasText(this.getErrorStack())) {
      errorStackString = this.getErrorStack().substring(0, 1000);
    }
    return MessageFormat.format(
            """
                    #### {0} \s
                    --- \s
                    ###### 报警时间：**{1}** \s
                    ###### 追踪序列：**{2}** \s
                    ###### 业务信息：**{3}** \s
                    ###### 异常信息：**{4}** \s
                    ###### 解决方案：**{5}** \s
                    ###### 堆栈信息：{6}\s
                    """
            , this.getTitle()
            , LocalDateTimeUtil.format(this.getHappenedTime(), "yyyy-MM-dd HH:mm:ss")
            , org.springframework.util.StringUtils.hasText(traceId) ? traceId : "无"
            , this.getBizData()
            , this.getErrorData()
            , this.getSolutionAction()
            , errorStackString
    );
  }
}
